package networkmapping;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeSelectionModel;

import options.view.ViewSettings;
import systemInformation.DeviceSnapshotPanel;
import features.SNMPClient;
import features.SNMPException;
import features.SNMPSession;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class MappingResultPanel extends JPanel {

	private static final long serialVersionUID = -9011337349875963442L;

	private JSplitPane leftSplitPane;
	private JTree hostTree;
	private JLabel descriptionLabel;
	private JPanel leftPanel;
	private JLabel discoveryHostLabel;
	private JSplitPane propertySplitPane;
	private DefaultMutableTreeNode root;

	private JScrollPane propertyScroll;

	private Host firstHost;

	public MappingResultPanel(Host firstHost) {
		super();
		this.firstHost = firstHost;
		init();
	}

	private void init() {
		ViewSettings.getSettings().setLookAndFeel(this);
		setLayout(new BorderLayout());
		setSize(400, 200);
		setVisible(true);
		this.setBackground(new java.awt.Color(255, 255, 255));

		{
			leftSplitPane = new JSplitPane();
			leftSplitPane.setDividerLocation(this.getWidth() / 2);
			this.add(leftSplitPane, BorderLayout.CENTER);
			leftSplitPane.setBackground(new java.awt.Color(255, 255, 255));
			setPropertyPanel();
			{
				leftPanel = new JPanel();
				leftPanel.setLayout(new BorderLayout());
				leftSplitPane.setLeftComponent(leftPanel);
				leftPanel.setBackground(new java.awt.Color(255, 255, 255));

				setDiscoveryHostLabel();
				setHostTree();
			}
		}

	}

	private void setPropertyPanel() {

		propertySplitPane = new JSplitPane();
		propertySplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		leftSplitPane.setRightComponent(propertySplitPane);
		propertySplitPane.setBackground(new java.awt.Color(255, 255, 255));
		addDescriptionLabel();

	}

	private void addTabbedPane(DeviceSnapshotPanel infoPanel) {
		
		JTabbedPane tabbedPane= new JTabbedPane();
		tabbedPane.add("Informazioni sulle interfacce", infoPanel.getPnlInterfaceInformation());
		tabbedPane.addTab("Tabella di Routing", infoPanel.getpnlHostInfo());
		tabbedPane.addTab("Informazioni sul sistema",infoPanel.getPnlBasicInformation());
		tabbedPane.addTab("Informazioni sulle periferiche", infoPanel.getPnlDeviceInformation());
		tabbedPane.addTab("Informazioni sui processi", infoPanel.getPnlProcessInformation());
		tabbedPane.addTab("Informazioni sul software installato", infoPanel.getPnlInstalledSwInformation());
		propertySplitPane.add(tabbedPane, JSplitPane.RIGHT);
		

	}

	private void addDescriptionLabel() {

		descriptionLabel = new JLabel();
		descriptionLabel.setText("Host Description");

		propertySplitPane.add(descriptionLabel, JSplitPane.LEFT);
		descriptionLabel.setBorder(BorderFactory.createTitledBorder(""));
		descriptionLabel.setBackground(new java.awt.Color(255, 255, 255));
		descriptionLabel.setFont(new java.awt.Font("Arial", 1, 11));

	}

	private void setDiscoveryHostLabel() {
		{
			discoveryHostLabel = new JLabel();
			discoveryHostLabel.setText("Discovery Host");
			discoveryHostLabel.setPreferredSize(new Dimension(100, 30));
			discoveryHostLabel.setBackground(new java.awt.Color(255, 255, 255));
			discoveryHostLabel.setBorder(BorderFactory.createTitledBorder(""));
			discoveryHostLabel.setFont(new java.awt.Font("Arial", 1, 11));
			leftPanel.add(discoveryHostLabel, BorderLayout.NORTH);

		}
	}

	private JTree setHostTree() {
		if (hostTree == null) {
			root = new DefaultMutableTreeNode(firstHost);

			createModeltree(root, firstHost.getHost_SNMPConnected(), firstHost
					.getHost_NOT_SNMPConnected());

			hostTree = new JTree(root);
			hostTree.setEditable(false);
			hostTree.setExpandsSelectedPaths(true);
			hostTree.setRowHeight(30);
			hostTree.getSelectionModel().setSelectionMode(
					TreeSelectionModel.SINGLE_TREE_SELECTION);

			hostTree.addTreeSelectionListener(new TreeSelectionListener() {

				public void valueChanged(TreeSelectionEvent e) {
					DefaultMutableTreeNode node = (DefaultMutableTreeNode) hostTree
							.getLastSelectedPathComponent();
					if (node != null) {
						Host selectedHost = (Host) node.getUserObject();
						if (selectedHost.isSupportSNMP()) {
							getAllProperties(selectedHost.getAnUpInterface());
						}

						else {
							showMessage("L'host selezionato non supporta il protocollo SNMP");
						}
					}

				}
			});

			setNodeIcon();

			JScrollPane treeView = new JScrollPane(hostTree);

			leftPanel.add(treeView, BorderLayout.CENTER);

		}
		return hostTree;
	}

	protected void getAllProperties(String ipAddress) {
		SNMPSession session;
		try {
			session = SNMPClient.startSession(SNMPClient.VERSION_2C);
			session.init(ipAddress, SNMPClient.DEFAULT_PORT_STRING,
					SNMPClient.DEFAULT_COMMUNITY);
			DeviceSnapshotPanel infoPanel = new DeviceSnapshotPanel();
			infoPanel.updateAllTable(session);

			addTabbedPane(infoPanel);

		} catch (SNMPException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			showMessage("Problemi nelle richieste SNMP");
		}

	}

	private void setNodeIcon() {

		hostTree.setCellRenderer(new DefaultTreeCellRenderer() {
			ImageIcon snmpIcon = new ImageIcon("images/router.png");
			ImageIcon notSnmpIcon = new ImageIcon("images/router_red.png");

			@Override
			public Component getTreeCellRendererComponent(JTree tree,
					Object value, boolean sel, boolean expanded, boolean leaf,
					int row, boolean hasFocus) {
				Component c = super.getTreeCellRendererComponent(tree, value,
						sel, expanded, leaf, row, hasFocus);

				Host host = (Host) ((DefaultMutableTreeNode) value)
						.getUserObject();

				if (!tree.isEnabled()) {
					setDisabledIcon((host.isSupportSNMP()) ? snmpIcon
							: notSnmpIcon);
				} else {
					setIcon((host.isSupportSNMP()) ? snmpIcon : notSnmpIcon);
				}

				if (host.isSupportSNMP())
					setText(host.getHostName());
				else
					setText("NOT_SNMP: " + host.getUpInterfaces().get(0));
				return c;
			}
		});

		// }
	}

	private void createModeltree(DefaultMutableTreeNode parentNode,
			ArrayList<Host> snmpHosts, ArrayList<Host> notSnmpHosts) {

		// Aggiungi quelli NOT_SNMP che saranno nodi foglia.
		if (notSnmpHosts != null) {
			for (int j = 0; j < notSnmpHosts.size(); j++) {
				Host host_NOT_SNMP = notSnmpHosts.get(j);
				DefaultMutableTreeNode node_NOT_SNMP = new DefaultMutableTreeNode(
						host_NOT_SNMP);
				parentNode.add(node_NOT_SNMP);
			}
		}
		if (snmpHosts != null) {
			for (int i = 0; i < snmpHosts.size(); i++) {
				Host host = snmpHosts.get(i);
				DefaultMutableTreeNode node = new DefaultMutableTreeNode(host);

				createModeltree(node, host.getHost_SNMPConnected(), host
						.getHost_NOT_SNMPConnected());

				parentNode.add(node);
			}
		}

	}

	private void showMessage(String message) {
		JOptionPane.showMessageDialog(this, message);
	}

}
